<template>
	<view class="page">
		<u-navbar placeholder>
			<template #left></template>
			<template #center>
				<text class="font-md-g font-weight-bolder">大力胶通告</text>
			</template>
		</u-navbar>
		<indexCardItem v-for="item in listData.list" :itemData="item" :key="item.viewID"></indexCardItem>
		<view  class="pb-3">
			<u-loadmore v-if="listData.status !== 'nomore'" :status="listData.status" />
		</view>
	</view>
</template>

<script setup>
	import { reactive } from 'vue'
	import{onLoad} from '@dcloudio/uni-app'
	import indexCardItem from '@/components/index_card_item.vue'
	import { getIndexList, getCoverList } from '@/common/api'
	// getViews()
	// getIndexList()
	// getCoverList()
	const listData = reactive({
		list: [],
		status: 'loading'
	})
	
	const getViewsDetail = async (item) => {
		return new Promise((success) => {
			setTimeout(async () => {
				const itemRes = await getIndexList({pageSize: 1, pageNum: 1, viewId: item.viewID, sort:{"field": "publishedDate", "order": "desc"}})	
				success(itemRes)
			}, 900)
		})
	}
	function getRandomInt(min, max) {
	  min = Math.ceil(min);
	  max = Math.floor(max);
	  return Math.floor(Math.random() * (max - min + 1)) + min;
	}
	// 获取首页资源
	// 首先要获取到试图的列表
	// 获取封面列表根据试图的id分配封面
	// 将试图列表id都进行一次请求记录分页条数可以给1减轻带宽压力主要获取总数 
	const getIndexData = async () => {
		listData.status = 'loading'
		// const viewList = await getViews()
		const coverList = await getCoverList()
		listData.status = 'nomore'
		listData.list = coverList.records
		listData.list = listData.list.filter(item => {
			return !!item.cover
		})
		for (let i = 0; i < listData.list.length; i ++) {
			const item = listData.list[i]
			const cover = item.cover
			console.log(cover)
			const index = (0, item.cover.length - 1)
			item.cover = cover[index].text
			// item.cover = itemRes.records[0].posterLink.title
		}
		
		for (let i = 0; i < listData.list.length; i ++) {
			const item = listData.list[i]
			const itemRes = await getViewsDetail(item)
			item.total = itemRes.total
			item.updateTime = itemRes.records[0] ? itemRes.records[0].publishedDate : ''
			// item.cover = itemRes.records[0].posterLink.title
		}
		listData.list = [...listData.list]
		// const indexList = await getIndexList({sort:{"field": "publishedDate", "order": "desc"}})
		// console.log(indexList)
	}
	onLoad(() => {
		getIndexData()
	})
</script>

<style lang="scss" scoped>
	.page {
		min-height: 100vh;
	}
</style>